<template>
    <div class="login">
        <!-- 登录页面 -->
        <div>
            <!-- 在使用表单验证时，需要注意：
            1.必须给表单对象绑定一个对象：:model="loginForm"，而且不能写成：v-model="loginForm"
            2.所有的子控件的绑定对象必须是表单对象的内部对象，比如：v-model="loginForm.username"，否则总是验证失败
            -->
            <el-form class="login-container" :model="loginForm" ref="loginForm" :rules="rules" label-position="left">
                <h3 class="login-title">用户登录</h3>
                <!-- <el-form-item class="login-dialog" prop="username" v-if="this.userPhone">
                    <el-input type="text" v-model="loginForm.userphone" auto-complete="off" placeholder="电话号码"></el-input>
                </el-form-item> -->
                <el-form-item class="login-dialog" prop="username">
                    <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="用户名 / 电话号码"></el-input>
                </el-form-item>
                <el-form-item class="login-dialog" prop="password">
                    <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码">
                </el-input>
                <el-form-item class="login-user">
                  <router-link to="/personal/login/registered">没有账号？</router-link>
                </el-form-item>
                <el-form-item class="login-forget">
                  <router-link to="/personal/login/pass-forget">忘记密码？</router-link>
                </el-form-item>
                </el-form-item>
                    <el-checkbox class="login-remember" v-model="checked">记住密码</el-checkbox>
                <el-form-item>
                  <router-link :to="to">
                    <el-button class="login-button" type="primary" @click="submitClick()">
                        登录
                    </el-button>
                  </router-link>
                </el-form-item>
            </el-form>
        <div class="feedback-prompt" v-if="this.appear">
            <span>账号或密码错误</span>
        </div>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
import { garbageMixin } from '../../utils/mixin'
import { setLocalStorage } from '../../utils/localStorage'
export default {
  mixins: [garbageMixin],
  name: 'Login',
  data () {
    return {
      appear: false,
      message: '账号或密码错误',
      rules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }
        ],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }
        ]
      },
      to: '',
      loginForm: {
        username: '',
        password: ''
      },
      checked: true
    }
  },
  methods: {
    submitClick (formName) {
      let data = qs.stringify({
        username: this.loginForm.username,
        password: this.loginForm.password
      })
      // 数据提交
      let prefix = '/user/login'
      axios.post(prefix, data)
        .then((response) => {
          let success = response.data.data
          console.log(response, 'uuu', response.status, success)

          if (response.data.code === 1) {
            this.$message({
              message: '登录成功',
              type: 'success'
            })
            let detailSave = response.data.data.user
            console.log('detail', detailSave)
            this.$router.push('/')
            this.setUserName(detailSave.username)
            this.setUserPhone(detailSave.phone)
            this.setUserAge(detailSave.age)
            this.setUserSex(detailSave.sex)
            this.setUserId(detailSave.id)
            this.setUserIntergration(detailSave.integration)
            this.setUserSessionId(success.sessionId)
            // 向本地缓存数据
            setLocalStorage('userName', detailSave.username)
            setLocalStorage('userPhone', detailSave.phone)
            setLocalStorage('userAge', detailSave.age)
            setLocalStorage('userSex', detailSave.sex)
            setLocalStorage('id', detailSave.id)
            setLocalStorage('sessionId', success.sessionId)
            setLocalStorage('userIntergration', detailSave.integration)

            localStorage.setItem('hh', 1111)
            let user = qs.stringify(detailSave)
            console.log(user, 'user')
            sessionStorage.setItem('id', detailSave.id)
            sessionStorage.setItem('url', detailSave.url)
            sessionStorage.setItem('sex', detailSave.sex)
            sessionStorage.setItem('age', detailSave.age)
            sessionStorage.setItem('username', detailSave.username)
            sessionStorage.setItem('password', detailSave.password)
            sessionStorage.setItem('phone', detailSave.phone)
            sessionStorage.setItem('integration', detailSave.integration)
            sessionStorage.setItem('sessionId', success.sessionId)
          } else {
            this.appear = true
            setTimeout(() => {
              this.appear = false
            }, 2000)
          }
        })
        .catch((error) => {
          console.log(error, 'kk')
        })
    }

  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/styles/global";
.login {
  .login-container {
        width: 100%;
        margin: 130px auto;
        background-clip: padding-box;
        padding: 35px 35px 15px 35px;
        box-sizing: border-box;
        border: 1px solid #eaeaea;
        border-radius: 15px;
        box-shadow: 0 0 25px #cac6c6;
        background: #fff;
        position: relative;
        .login-dialog {
            margin: 20px 0;
        }
        .login-title {
            margin: 0 auto 40px auto;
            text-align: center;
            color: #505458;
        }
        .login-user {
          position: absolute;
          right: 0px;
          top: 72px;
          a {
            color: #555;
            text-decoration: none;
          }
        }
        .login-forget {
          position: absolute;
          right: 82px;
          top:72px;
          a {
            color: #555;
            text-decoration: none;
          }

        }
        .login-remember {
            margin: 20px 0 10px 0;
            text-align: center;
        }
        .login-button {
            width: 100%;
            margin-bottom: 12px;
            a {
                color: #fff;
                text-decoration: none;
            }
        }
  }
  .feedback-prompt {
    background: #000;
    color: white;
    opacity: 0.4;
    font-size: 14px;
    width: 160px;
    height: 54px;
    line-height: 54px;
    border-radius: 10px;
    text-align: center;
    position: absolute;
    top: 270px;
    left: 100px;
  }
}
</style>
